package com.sf.appointment.service.impl;

import java.util.Date;
import java.util.List;


import com.sf.appointment.domain.BusStatement;
import com.sf.appointment.mapper.BusStatementMapper;
import com.sf.appointment.utils.RegexUtils;
import com.sf.appointment.utils.VehiclePlateNoUtil;
import com.sf.appointment.vo.BusAppointmentVo;
import com.sf.common.utils.DateUtils;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sf.appointment.mapper.BusAppointmentMapper;
import com.sf.appointment.domain.BusAppointment;
import com.sf.appointment.service.IBusAppointmentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 养修信息预约Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@Service
@Transactional
public class BusAppointmentServiceImpl implements IBusAppointmentService
{
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;

    @Autowired
    private BusStatementMapper busStatementMapper;

    /**
     * 查询养修信息预约
     *
     * @param id 养修信息预约主键
     * @return 养修信息预约
     */
    @Override
    public BusAppointment selectBusAppointmentById(Long id)
    {
        return busAppointmentMapper.selectBusAppointmentById(id);
    }

    /**
     * 查询养修信息预约列表
     *
     * @param busAppointment 养修信息预约
     * @return 养修信息预约
     */
    @Override
    public List<BusAppointment> selectBusAppointmentList(BusAppointment busAppointment)
    {
        return busAppointmentMapper.selectBusAppointmentList(busAppointment);
    }

    /**
     * 新增养修信息预约
     *
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int insertBusAppointment(BusAppointmentVo busAppointment)
    {

        Assert.notNull(busAppointment.getCustomerName(),"用户名不能为空");
        Assert.notNull(busAppointment.getCustomerPhone(),"手机号不能为空");
        Assert.notNull(busAppointment.getServiceType(),"服务类型不能为空");
        Assert.notNull(busAppointment.getAppointmentTime(),"预约时间不能为空");
        Assert.notNull(busAppointment.getInfo(),"备注信息不能为空");
        Assert.notNull(busAppointment.getLicensePlate(),"车牌号不能为空");
        Assert.notNull(busAppointment.getCarSeries(),"汽车类型不能为空");





        busAppointment.setCreateTime(DateUtils.getNowDate());
        boolean phoneLegal = RegexUtils.isPhoneLegal(busAppointment.getCustomerPhone());
        if(!phoneLegal){
            throw new IllegalArgumentException("手机号码不正确");
        }
        //车牌号码校验
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(busAppointment.getLicensePlate());
        if(vehiclePlateNo == null){
            throw new IllegalArgumentException("车牌号不正确");
        }

        //做合理化校验,日期校验
        Date appointmentTime = busAppointment.getAppointmentTime();
        Date nowDate = DateUtils.getNowDate();
        boolean flag = appointmentTime.before(nowDate);
//        Assert.state(flag,"预约时间不合理");
        if(flag){
            throw new IllegalArgumentException("预约时间不合理");
        }

        BusAppointment bus = new BusAppointment();
        BeanUtils.copyProperties(busAppointment,bus);
        return busAppointmentMapper.insertBusAppointment(bus);
    }

    /**
     * 修改养修信息预约
     *
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int updateBusAppointment(BusAppointmentVo busAppointment)
    {

        Assert.notNull(busAppointment.getCustomerName(),"用户名不能为空");
        Assert.notNull(busAppointment.getCustomerPhone(),"手机号不能为空");
        Assert.notNull(busAppointment.getServiceType(),"服务类型不能为空");
        Assert.notNull(busAppointment.getAppointmentTime(),"预约时间不能为空");
        Assert.notNull(busAppointment.getInfo(),"备注信息不能为空");
        Assert.notNull(busAppointment.getLicensePlate(),"车牌号不能为空");
        Assert.notNull(busAppointment.getCarSeries(),"汽车类型不能为空");



        boolean phoneLegal = RegexUtils.isPhoneLegal(busAppointment.getCustomerPhone());
        if(!phoneLegal){
            throw new IllegalArgumentException("手机号码不正确");
        }
        //车牌号码校验
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(busAppointment.getLicensePlate());
        if(vehiclePlateNo == null){
            throw new IllegalArgumentException("车牌号不正确");
        }

        //做合理化校验,日期校验
        Date appointmentTime = busAppointment.getAppointmentTime();
        Date nowDate = DateUtils.getNowDate();
        boolean flag = appointmentTime.before(nowDate);
//        Assert.state(flag,"预约时间不合理");
        if(flag){
            throw new IllegalArgumentException("预约时间不合理");
        }

        BusAppointment bus = new BusAppointment();
        BeanUtils.copyProperties(busAppointment,bus);

        BusAppointment busAppointment1 = busAppointmentMapper.selectBusAppointmentById(bus.getId());
        if(busAppointment1 != null){
            if(busAppointment1.getStatus()==0){
                return busAppointmentMapper.updateBusAppointment(bus);
            }else {
                throw new RuntimeException("状态错误");
            }

        }else{
            throw new RuntimeException("错误");
        }
    }

    /**
     * 批量删除养修信息预约
     *
     * @param ids 需要删除的养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentByIds(Long[] ids)
    {
        return busAppointmentMapper.deleteBusAppointmentByIds(ids);
    }

    /**
     * 删除养修信息预约信息
     *
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentById(Long id)
    {
        if (id==null){
            throw new RuntimeException("id不存在");
        }else{
            BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(id);
            if (busAppointment!=null){
                if(busAppointment.getStatus()==BusAppointment.STATUS_APPOINTMENT||busAppointment.getStatus()==BusAppointment.STATUS_CANCEL||busAppointment.getStatus()==BusAppointment.STATUS_OVERTIME){
                    return busAppointmentMapper.deleteBusAppointmentById(id);
                }else {
                    throw new RuntimeException("状态不合法");
                }

            }else {
                throw new RuntimeException("id不合法");
            }
        }

    }

    @Override
    public int arralShop(Long id) {
        if (id!=null){
            BusAppointment busAppointment1 = busAppointmentMapper.selectBusAppointmentById(id);
            if (busAppointment1!=null){
                Integer status = busAppointment1.getStatus();
                if (status==BusAppointment.STATUS_APPOINTMENT){
                    busAppointment1.setStatus(BusAppointment.STATUS_ARRIVAL);
                    busAppointment1.setActualArrivalTime(DateUtils.getNowDate());
                    return busAppointmentMapper.updateBusAppointment(busAppointment1);
                }else {
                    throw new RuntimeException("当前状态错误");
                }
            }else {
                throw new RuntimeException("操作失败");
            }
        }else{
            throw new RuntimeException("id出现错误");
        }
    }

    @Override
    public int cancel(Long id) {
        if (id!=null){
            BusAppointment busAppointment1 = busAppointmentMapper.selectBusAppointmentById(id);
            if (busAppointment1!=null){
                Integer status = busAppointment1.getStatus();
                if (status==BusAppointment.STATUS_APPOINTMENT){
                    busAppointment1.setStatus(BusAppointment.STATUS_CANCEL);
                    return busAppointmentMapper.updateBusAppointment(busAppointment1);
                }else {
                    throw new RuntimeException("当前状态错误");
                }
            }else {
                throw new RuntimeException("操作失败");
            }
        }else{
            throw new RuntimeException("id出现错误");
        }
    }

    @Override
    public Long generate(Long id) {
        BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        if (busAppointment==null){
            throw new RuntimeException("id不合法");
        }
        Integer status = busAppointment.getStatus();
        if(BusAppointment.STATUS_ARRIVAL.equals(status)||BusAppointment.STATUS_SETTLE.equals(status)||BusAppointment.STATUS_PAYED.equals(status)){
            BusStatement busStatement = busStatementMapper.selectBusStatementByAppointmentId(busAppointment.getId());
            if (busStatement==null){
                BusStatement bus = new BusStatement();
                bus.setCustomerName(busAppointment.getCustomerName());
                bus.setCustomerPhone(busAppointment.getCustomerPhone());
                bus.setServiceType(busAppointment.getServiceType());
                bus.setLicensePlate(busAppointment.getLicensePlate());
                bus.setCarSeries(busAppointment.getCarSeries());
                bus.setInfo(busAppointment.getInfo());
                bus.setActualArrivalTime(busAppointment.getActualArrivalTime());
                bus.setAppointmentId(busAppointment.getId());
                busStatementMapper.insertBusStatement(bus);
                busAppointment.setStatus(BusAppointment.STATUS_SETTLE);
                busAppointmentMapper.updateBusAppointment(busAppointment);
                return bus.getId();
            }else{
                busAppointment.setStatus(BusAppointment.STATUS_SETTLE);
                busAppointmentMapper.updateBusAppointment(busAppointment);
                return busStatement.getId();
            }
        }else {
            throw new RuntimeException("状态不合法");
        }
    }
}
